const WebSocket = require('ws');
const wss = new WebSocket.Server({ port: 8081 });

let clients = [
    // 这里可以存放已连接的客户端
];

wss.on('connection', (ws) => {
    clients.push(ws);

    ws.on('message', (message) => {
        // 广播接收到的消息给所有客户端
        clients.forEach(client => {
            if (client !== ws && client.readyState === WebSocket.OPEN) {
                client.send(message);
            }
        });
    });

    ws.on('close', () => {
        clients = clients.filter(client => client !== ws);
    });
});

console.log('WebSocket server is running on ws://localhost:8081');